//
//  AdvertisementViewModel.swift
//  FeastBooking
//
//  Created by Imon on 2017/10/2.
//  Copyright © 2017年 Imon. All rights reserved.
//

import Foundation
import RxSwift
import Moya

class AdvertisementViewModel {
    
    public static let share = AdvertisementViewModel()
    
    private var provider: RxMoyaProvider<AdvertisementApi>
    
    private init() {
        let endpointClosure = { (target: AdvertisementApi) -> Endpoint<AdvertisementApi> in
            let defaultEndpoint = RxMoyaProvider.defaultEndpointMapping(for: target)
            return defaultEndpoint.adding(newHTTPHeaderFields: ["content-type": "application/json"])
        }
        provider = RxMoyaProvider<AdvertisementApi>(endpointClosure: endpointClosure)
    }
    
    func getAdvertisements() -> Observable<[AdvertisementResponse]> {
        return provider.request(.getAdvertisements())
               .mapJSON()
               .mapArray(type: AdvertisementResponse.self)
    }
}
